/****************************************************************************
 * Copyright (C) 2025 Chenyz. All rights reserved.
 *
 * @file    TinyOS_Types.h
 *
 * @par     dependencies
 *          - stdint.h (Standard integer types)
 *          - TinyOS_Config.h (Configuration parameters)
 *          - TinyOS_Lib.h (Core library definitions)
 *
 * @author  Chenyz
 *
 * @brief   TinyOS Type Definitions Header
 *          This file contains all type definitions, enumerations, and data structures
 *          used throughout the TinyOS real-time operating system. It defines:
 *          - Task states and synchronization primitives
 *          - Kernel object data structures
 *          - Error codes and event types
 *          - Configuration constants and flags
 *
 * @version V1.0
 * @date    2025-10-03
 *
 * @note    This header provides the fundamental type system for TinyOS components
 *          and should be included by all kernel modules and application code.
 ****************************************************************************/

#ifndef __TINYOS_TYPES_H__
#define __TINYOS_TYPES_H__

/******************************** Includes *********************************/
#include <stdint.h>
#include "TinyOS_Config.h"
#include "TinyOS_Lib.h"

/******************************** Defines **********************************/

/* Task state definitions */
#define     TINYOS_TASK_READY               (0U)
#define     TINYOS_TASK_DELETED             (1U << 1)
#define     TINYOS_TASK_DELAYED             (1U << 2)
#define     TINYOS_TASK_SUSPENDED           (1U << 3)
#define     TINYOS_TASK_WAIT_MASK           (0xFFU << 16)

/* Mailbox operation modes */
#define     TINYOS_MAILBOX_SEND_NORMAL      (0U)
#define     TINYOS_MAILBOX_SEND_FRONT       (1U)

/* Flag group operation modes */
#define     TINYOS_FLAGGROUP_CLEAR          (0U << 0)
#define     TINYOS_FLAGGROUP_SET            (1U << 0)
#define     TINYOS_FLAGGROUP_ANY            (0U << 1)
#define     TINYOS_FLAGGROUP_ALL            (1U << 1)
#define     TINYOS_FLAGGROUP_CONSUME        (1U << 7)

/* Combined flag group operations */
#define     TINYOS_FLAGGROUP_CLEAR_ALL      (TINYOS_FLAGGROUP_CLEAR | TINYOS_FLAGGROUP_ALL)
#define     TINYOS_FLAGGROUP_CLEAR_ANY      (TINYOS_FLAGGROUP_CLEAR | TINYOS_FLAGGROUP_ANY)
#define     TINYOS_FLAGGROUP_SET_ALL        (TINYOS_FLAGGROUP_SET | TINYOS_FLAGGROUP_ALL)
#define     TINYOS_FLAGGROUP_SET_ANY        (TINYOS_FLAGGROUP_SET | TINYOS_FLAGGROUP_ANY)

/* Timer configuration types */
#define     TINYOS_TIMER_CONFIGTYPE_TASK    (1U << 0)
#define     TINYOS_TIMER_CONFIGTYPE_INT     (0U << 0)

/***************************** Type Definitions ****************************/

/* Error code enumeration */
typedef enum {
    TinyOS_ErrorType_NoError = 0U,
    TinyOS_ErrorType_Timeout,  
    TinyOS_ErrorType_SourceUnavailable,
    TinyOS_ErrorType_Delete,
    TinyOS_ErrorType_SourceFull,
    TinyOS_ErrorType_OwnerError
} TinyOS_ErrorType;

/* Event type enumeration */
typedef enum {
    TinyOS_EventTypeUnKnow = 0U,
    TinyOS_EventTypeSemaphore,
    TinyOS_EventTypeMailBox,
    TinyOS_EventTypeMemBlock,
    TinyOS_EventTypeFlagGroup,
    TinyOS_EventTypeMutex
} TinyOS_EventType;

/* Timer state enumeration */
typedef enum {
    TinyOS_TimerStateCreated = 0U,
    TinyOS_TimerStateStarted,
    TinyOS_TimerStateRunning,
    TinyOS_TimerStateStopped,
    TinyOS_TimerStateDeleted
} TinyOS_TimerStateType;

/***************************** Structure Definitions ****************************/

/* Event block for task synchronization */
typedef struct {
    TinyOS_EventType    EventType;
    TinyOS_ListType     WaitList;
} TinyOS_EventBlockType;

/* Semaphore data structure */
typedef struct {
    uint32_t                SemCount;
    uint32_t                SemMaxCount;
    TinyOS_EventBlockType   EventBlock;
} TinyOS_SemaphoreType;

/* Semaphore information structure */
typedef struct {
    uint32_t                SemCount;
    uint32_t                SemMaxCount;
    uint32_t                TaskCount;
} TinyOS_SemaphoreInfo;

/* Mailbox data structure */
typedef struct {
    uint32_t                MailCount;
    uint32_t                MaxCount;
    uint32_t                ReadIndex;
    uint32_t                WriteIndex;
    void**                  MsgBuffer;
    TinyOS_EventBlockType   EventBlock;
} TinyOS_MailBoxType;

/* Mailbox information structure */
typedef struct {
    uint32_t                MailCount;
    uint32_t                MailBoxMaxCount;
    uint32_t                TaskCount;
} TinyOS_MailBoxInfo;

/* Memory block pool data structure */
typedef struct {
    void*                   MemStartAddr;
    uint32_t                MaxMemBlockCount;
    uint32_t                MemBlockSize;
    TinyOS_ListType         MemBlockList;
    TinyOS_EventBlockType   EventBlock;
} TinyOS_MemBlockType;

/* Memory block information structure */
typedef struct {
    uint32_t                MemBlockCount;
    uint32_t                MaxMemBlockCount;
    uint32_t                MemBlockSize;
    uint32_t                TaskCount;
} TinyOS_MemBlockInfo;

/* Flag group data structure */
typedef struct {
    uint32_t                FlagGroupMask;
    TinyOS_EventBlockType   EventBlock;
} TinyOS_FlagGroupType;

/* Flag group information structure */
typedef struct {
    uint32_t                FlagGroupMask;
    uint32_t                TaskCount;
} TinyOS_FlagGroupInfo;

/* Timer control block */
typedef struct {
    TinyOS_NodeType         LinkNode;
    uint32_t                StartDelayTicks;
    uint32_t                PeriodTicks;
    uint32_t                DelayTicks;
    void (*pfTimerCallbacks)(void* param);
    void*                   param;
    uint32_t                TimerConfig;
    TinyOS_TimerStateType   TimerState;
} TinyOS_TimerType;

/* Timer information structure */
typedef struct {
    uint32_t                StartDelayTicks;
    uint32_t                PeriodTicks;
    void (*pfTimerCallbacks)(void* param);
    void*                   param;
    uint32_t                TimerConfig;
    TinyOS_TimerStateType   TimerState;
} TinyOS_TimerInfoType;

/* Task stack type definition */
typedef uint32_t TinyOS_TaskStackType;

/* Task control block - core task management structure */
typedef struct __TinyOS_TaskType {
    TinyOS_TaskStackType*   TaskStack;
    uint32_t*               TaskStackBase;
    uint32_t                TaskStackSize;				
    TinyOS_NodeType         TaskDelayListNode;
    TinyOS_NodeType         TaskListNode;
    uint32_t                TaskDelayTicks;
    uint8_t                 TaskPrioity;
    uint8_t                 TaskStatus;
    uint8_t                 TaskSlice;
    uint8_t                 TaskSuspendedCount;
    uint8_t                 TaskDeleteFlag;
    void*                   Deleteparam;   
    void(*TaskDeleteFunc)(void* Deleteparam);
    TinyOS_EventBlockType*  TaskEventBlock;
    void*                   TaskEventMsg;
    uint32_t                TaskEventWaitResult;
    uint32_t                TaskWaitFlagGroupType;
    uint32_t                TaskFlagGroupRequestMask;
} TinyOS_TaskType;

/* Task information structure */
typedef struct {
    uint32_t                TaskInfoDelayTicks;
    uint8_t                 TaskInfoPrioity;
    uint8_t                 TaskInfoStatus;
    uint8_t                 TaskInfoSlice;
    uint8_t                 TaskInfoSuspendedCount;
    uint32_t                TaskStackSizeFree;
    uint32_t                TaskStackSize;	
} TinyOS_TaskInfo;

/* Mutex data structure with priority inheritance */
typedef struct {
    uint32_t                LockCount;
    uint32_t                OwnerTaskPriority;
    TinyOS_TaskType*        OwnerTask;    
    TinyOS_EventBlockType   EventBlock;
} TinyOS_MutexType;

/* Mutex information structure */
typedef struct {
    uint32_t                TaskCount;
    uint32_t                LockCount;
    uint32_t                OwnerTaskPriority;
    uint32_t                InheritedPriority;
    TinyOS_TaskType*        OwnerTask;    
} TinyOS_MutexInfoType;

#endif /* __TINYOS_TYPES_H__ */
